<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Create_account_ewallet extends MY_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->helper(array('form', 'url'));
		$this->load->library(array('form_validation', 'uri'));
		$this->load->model("ewallet_model", "model", TRUE);
	}

	function index(){
		$data['countries'] = $this->model->get_countries();
		$data['logged'] = $this->logged;
		$data['submenus'] = $this->sub_menus;
		$this->template->load("layout_register_ewallet", "create_account_ewallet_view", $data);
	}

	private function email_check($str)
	{
		$r = $this->model->check_email_exist($str);
		switch($r){
			case -1 : {
				//$this->form_validation->set_message('email_check', 'This e-mail address is already registered with KashFX.com eWallet');
				return FALSE;
			}
			case 1:{
				return TRUE;
			}
		}
	}
	
	function mail_check() {
		$mail = $this->input->post("email");
		$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
		if ($this->form_validation->run() == FALSE){
			$data['result'] = false;
			$data['message'] = form_error("email");
			$data['message'] = strip_tags($data['message']);
		}
		else{
			$data['result'] = $this->email_check($mail);
			if($data['result']) {
				$data['message'] = "Email address accepted.";
			}
			else {
				$data['message'] = "This e-mail address is already registered with KashFX.com eWallet.";
			}
		}
//		echo $this->model->get_country("");
		echo json_encode($data);
	}
	
	function save(){
		$this->form_validation->set_rules('accountType', 'Account Type', 'required');
			
				/*rules for company name*/
		if($this->input->post('accountType') == "business"){
			$this->form_validation->set_rules('organizationName', 'Organization Name', 'required|min_length[5]|max_length[80]');
		}
		
		$this->form_validation->set_rules('countryName', 'Country', 'trim|required');
		$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
		$this->form_validation->set_rules('phoneCountryCode', 'Mobile Phone Country Code', 'trim|required|max_length[6]|is_natural');
		$this->form_validation->set_rules('phoneNumberMobile', 'Mobile Phone Number', 'trim|required|max_length[10]|numeric');
		
		$this->form_validation->set_rules('firstName', 'First Name ', 'required|min_length[2]|max_length[30]|alpha_dash');
		$this->form_validation->set_rules('lastName', 'Last Name', 'required|min_length[2]|max_length[30]|alpha_dash');

		$this->form_validation->set_rules('password', 'Password', 'required|min_length[8]|max_length[30]');
		$this->form_validation->set_rules('passwordVerify', 'Re-enter password', 'required|matches[password]');

	
		if ($this->form_validation->run() == FALSE){
//			$data['error'] = validation_errors();
			if($this->input->post('accountType') == "business"){
				$data['error']['organizationName'] = strip_tags(form_error('organizationName'));
			}
			
			$data['error']['countryName'] = strip_tags(form_error('countryName'));
			$data['error']['email'] = strip_tags(form_error('email'));
			$data['error']['phoneCountryCode'] = strip_tags(form_error('phoneCountryCode'));
			$data['error']['phoneNumberMobile'] = strip_tags(form_error('phoneNumberMobile'));
			$data['error']['firstName'] = strip_tags(form_error('firstName'));
			$data['error']['lastName'] = strip_tags(form_error('lastName'));
			$data['error']['password'] = strip_tags(form_error('password'));
			$data['error']['passwordVerify'] = strip_tags(form_error('passwordVerify'));
			
			$data['link'] = site_url('create_account_ewallet#message');
			$data['error']['message'] = "Some information is incorrect or missing. Please correct your entries and try again.";
		}
		else{
			
			$user = new User_ewallet();

			$user->USER_TYPE = "EW";
			$user->ACCOUNT_TYPE = $this->input->post('accountType');
			$user->USER_PASSWD = sha1($this->input->post('password'));
			$user->COMP_NAME = $this->input->post('organizationName');
			$user->FIRST_NAME = $this->input->post('firstName');
			$user->LAST_NAME = $this->input->post('lastName');
			$user->USER_ID = $this->input->post('email');

			$user->MOBILE_PHONE = $this->input->post('phoneCountryCode') . "-" . $this->input->post('phoneNumberMobile');
			$user->COUNTRY_CODE = $this->input->post('countryName');
			$user->LAST_IP = $this->input->ip_address();


			$data['link'] = site_url('create_account_ewallet#message');
			if($this->model->save($user)){
				$data['success'] = "Your <b>eWallet Account </b> was created successfully!";
			}
			else{
				$data['error']['message'] = "User account couldn't be created";
			}
			 
		}
		echo json_encode($data);
	}
}
